package com.qin.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qin.mall.common.api.CommonResult;
import com.qin.mall.common.api.MyBatisPlusPage;
import com.qin.mall.domain.PmsBrand;
import com.qin.mall.dto.BrandDto;
import com.qin.mall.service.PmsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 品牌管理
 * @author Leisure
 * create 2022 2022/11/5 14:57
 **/
@RestController
@RequestMapping("/brandManage")
public class BrandController {
    @Autowired
    private PmsBrandService brandService;

    //TODO 查询所有品牌
    @GetMapping("/getBrandList")
    public CommonResult<List<PmsBrand>> getBrandList$(){
        return CommonResult.success(brandService.listAllBrand());
    }

    /**
     * 测试用例:
     * {
     *     "name":"特步",
     *     "factoryStatus":1,
     *     "showStatus":1
     * }
     * @param brandDto
     * @return
     */
    //TODO 创建新的品牌
    @PostMapping("/insetBrand")
    public CommonResult insetBrand$(@RequestBody BrandDto brandDto){
        int count = brandService.createBrand(brandDto);
        CommonResult commonResult;
        if (count!=0){
            commonResult = CommonResult.success(count);
        }else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }

    /**
     * 测试用例:
     * {
     *     "name":"安踏",
     *     "factoryStatus":1,
     *     "showStatus":1
     * }
     * @param id
     * @param brandDto
     * @return
     */
    // TODO 更新品牌
    @PostMapping("/updateBrand")
    public CommonResult updateBrand$(@RequestParam(value = "id") Long id,
                              @RequestBody BrandDto brandDto){
        int count = brandService.updateBrand(id,brandDto);
        CommonResult commonResult;
        if (count!=0){
            commonResult = CommonResult.success(count);
        }else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }

    // TODO 通过id删除品牌
    @GetMapping("/deleteBrandById/{id}")
    public CommonResult deleteBrand$(@PathVariable("id") Long id){
        int count = brandService.deleteBrand(id);
        if (count == 1) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    //TODO 批量删除
    /** 测试用例:
     * [73,74]
     * postman传递集合参数 用数组[1,2]这样
     * @param ids
     * @return
     */
    @PostMapping("/deleteBrandByIdS")
    public CommonResult deleteBrands$(@RequestBody List<Long> ids){
        int count = brandService.deleteBrands(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 测试用例:
     * pageNum:2
     * pageSize:2
     * @param pageNum
     * @param pageSize
     * @return
     */
    //TODO 品牌分页列表
    @GetMapping("/selectListBrand")
    public CommonResult<MyBatisPlusPage<PmsBrand>> selectListBrand$(@RequestParam(value = "keyword", required = false) String keyword,
                                                                    @RequestParam(value = "pageNum")int pageNum,
                                                                    @RequestParam(value = "pageSize") int pageSize){
        Page<PmsBrand> brand = brandService.listBrand(keyword,pageNum, pageSize);
        
        return CommonResult.success(MyBatisPlusPage.mpPage(brand));
    }
    
    //TODO 通过id获取品牌
    @GetMapping("selectOnId/{id}")
    public CommonResult<PmsBrand> selectById$(@PathVariable("id") Long id){
        return CommonResult.success(brandService.getBrand(id));
    }

    //TODO 批量更新显示状态
    @PostMapping("/updateOnShowStatus")
    public CommonResult updateShowStatus$(@RequestParam("ids") List<Long> ids, 
                                   @RequestParam(value = "showStatus") Integer showStatus){
        int count = brandService.updateShowStatus(ids, showStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    //TODO 批量更新厂家制造商状态
    @PostMapping("/updateOnFactoryStatus")
    public CommonResult updateFactoryStatus$(@RequestParam("ids") List<Long> ids,
                                   @RequestParam(value = "factoryStatus") Integer factoryStatus){
        int count = brandService.updateFactoryStatus(ids, factoryStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    
}
